package com.alex.springbootdemo.mapper.mbg;

import com.alex.springbootdemo.model.pojo.UmsAdminRoleRelation;
import com.alex.springbootdemo.model.pojo.UmsPermission;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface UmsAdminRoleRelationMapper extends Mapper<UmsAdminRoleRelation> {

    @Select("SELECT\n" +
            "            p.*\n" +
            "        FROM\n" +
            "            ums_admin_role_relation ar\n" +
            "            LEFT JOIN ums_role r ON ar.role_id = r.id\n" +
            "            LEFT JOIN ums_role_permission_relation rp ON r.id = rp.role_id\n" +
            "            LEFT JOIN ums_permission p ON rp.permission_id = p.id\n" +
            "        WHERE\n" +
            "            ar.admin_id = #{adminId}\n" +
            "            AND p.id IS NOT NULL\n" +
            "            AND p.id NOT IN (\n" +
            "                SELECT\n" +
            "                    p.id\n" +
            "                FROM\n" +
            "                    ums_admin_permission_relation pr\n" +
            "                    LEFT JOIN ums_permission p ON pr.permission_id = p.id\n" +
            "                WHERE\n" +
            "                    pr.type = - 1\n" +
            "                    AND pr.admin_id = #{adminId}\n" +
            "            )\n" +
            "        UNION\n" +
            "        SELECT\n" +
            "            p.*\n" +
            "        FROM\n" +
            "            ums_admin_permission_relation pr\n" +
            "            LEFT JOIN ums_permission p ON pr.permission_id = p.id\n" +
            "        WHERE\n" +
            "            pr.type = 1\n" +
            "            AND pr.admin_id = #{adminId}")
    List<UmsPermission> getPermissionListByUserId(@Param("adminId") Long adminId);

}